package com.android.love.adapter;

import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.love.R;
import com.android.love.util.Constant;
import com.android.love.util.ImageUtils;
import com.android.love.util.LogUtil;
import com.android.love.vo.Goods;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

public class CartAdapter extends BaseAdapter implements Constant {
	private static final String TAG = "CommentAdapter";
	private ImageLoader loader;
	private List<Goods> goods = Collections.emptyList();;
	private Context context;
	private Handler handler;

	public CartAdapter(Context context, ImageLoader loader, List<Goods> goods,
			Handler handler) {
		super();
		this.context = context;
		this.loader = loader;
		this.goods = goods;
		this.handler = handler;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return goods.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return goods.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// ViewHolder holder;
		// if (convertView == null) {
		convertView = View.inflate(context, R.layout.cart_item, null);
		// holder = new ViewHolder();
		final CheckBox checkBox = (CheckBox) convertView
				.findViewById(R.id.checkBox);
		ImageView iv_cart_img = (ImageView) convertView
				.findViewById(R.id.iv_cart_img);
		ImageView iv_add = (ImageView) convertView.findViewById(R.id.iv_add);
		ImageView iv_reduce = (ImageView) convertView
				.findViewById(R.id.iv_reduce);
		TextView tv_cart = (TextView) convertView.findViewById(R.id.tv_cart);
		TextView tv_format = (TextView) convertView
				.findViewById(R.id.tv_format);
		final TextView tv_price = (TextView) convertView
				.findViewById(R.id.tv_price);
		final TextView tv_num = (TextView) convertView
				.findViewById(R.id.tv_num);
		TextView tv_id = (TextView) convertView.findViewById(R.id.tv_id);
		// convertView.setTag(holder);
		// } else {
		// holder = (ViewHolder) convertView.getTag();
		// }
		// Goods good = new Goods();
		final Goods good = goods.get(position);
		tv_cart.setText(good.getGoods_name());
		// tv_price.setText("￥" + good.getPrice() * good.getCount() + "");

		tv_price.setText("￥"
				+ (float) Math.round((float) (good.getPrice() * good.getCount() * 100))
				/ 100 + "");
		tv_id.setText("编号:" + good.getGoods_id());
		tv_num.setText(good.getCount() + "");
		checkBox.setChecked(good.isChecked());
		StringBuilder sb = new StringBuilder();
		if (good.getColor().get(0) != null)
			sb.append(good.getColor().get(0) + " ");
		if (good.getSize().get(0) != null)
			sb.append(good.getSize().get(0) + " ");
		if (good.getFormat().get(0) != null)
			sb.append(good.getFormat().get(0) + " ");

		tv_format.setText(sb.toString());
		loader.displayImageForSize(good.getGoods_image(), iv_cart_img,
				ImageUtils.getOptions(R.drawable.zw7),
				new SimpleImageLoadingListener(), 10, 10, 10, 10, 0, 0, 4f,
				true);
		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				LogUtil.i(TAG, "onCheckedChanged");
				good.setChecked(isChecked);
				// checkBox.setChecked(good.isChecked());
				sendMessage(CART_CHECK, position);
			}
		});
		iv_add.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				LogUtil.i(TAG, "iv_add");
				int num = good.getCount();
				++num;
				good.setCount(num);
				tv_num.setText(num + "");
				tv_price.setText("￥" + good.getPrice() * good.getCount() + "");
				sendMessage(CART_ADD, position);
			}
		});
		iv_reduce.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				LogUtil.i(TAG, "iv_reduce");
				int num = good.getCount();
				if (num > 1) {
					--num;
					good.setCount(num);
					tv_num.setText(num + "");
					tv_price.setText("￥" + good.getPrice() * good.getCount()
							+ "");
					sendMessage(CART_REDUCE, position);
				}
			}
		});
		return convertView;
	}

	/**
	 * 发消息
	 * 
	 * @param what
	 * @param position
	 */
	private void sendMessage(int what, int position) {
		Message msg = Message.obtain();
		msg.what = what;
		msg.arg1 = position;
		handler.sendMessage(msg);
	}

	// public static class ViewHolder {
	// CheckBox checkBox;
	// ImageView iv_cart_img;
	// ImageView iv_add;
	// ImageView iv_reduce;
	// TextView tv_cart;
	// TextView tv_format;
	// TextView tv_price;
	// TextView tv_num;
	// TextView tv_id;
	// }

}
